package com.cory.module.msg.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.core.annotation.AnnotationUtils;
import com.beust.jcommander.internal.Maps;
import com.cory.boot.utils.SpringUtils;
import com.cory.module.msg.admin.annotation.AdminMsgSettings;
import com.google.common.collect.Lists;

/**
 * @author xiongcong
 * @date 2022-09-17
 */
public class AdminMsgClient {

    private static Map<Class<? extends AdminMsg>, AdminMsg> msgClassMap = Maps.newHashMap();
    
    private static Map<String, AdminMsg> msgKeyMap = Maps.newHashMap();
    
    static {
        init();
    }
    
    public static void init() {
        List<AdminMsg> services = new ArrayList<>(SpringUtils.getContext().getBeansOfType(AdminMsg.class).values());
        if (services != null && services.size() > 0) {
            for (AdminMsg service : services) {
                AdminMsgSettings msgSettings = AnnotationUtils.findAnnotation(service.getClass(), AdminMsgSettings.class);
                msgClassMap.put(service.getClass(), service);
                msgKeyMap.put(msgSettings.key(), service);
            }
        }
    }
    
    public static List<AdminMsg> getAllAdminMsgs() {
        return Lists.newArrayList(msgClassMap.values());
    }
    
    public static AdminMsg getAdminMsg(Class<? extends AdminMsg> clazz) {
        return msgClassMap.get(clazz);
    }

    public static AdminMsg getAdminMsg(String msgKey) {
        return msgKeyMap.get(msgKey);
    }
    
    public static long getMsg(Class<? extends AdminMsg> clazz) {
        return getAdminMsg(clazz).getMsg();
    }
    
    public static void setMsg(Class<? extends AdminMsg> clazz, long num) {
        getAdminMsg(clazz).setMsg(num);
    }
    
    public static void addMsg(Class<? extends AdminMsg> clazz, long num) {
        getAdminMsg(clazz).addMsg(num);
    }

    public static void reduceMsg(Class<? extends AdminMsg> clazz, long num) {
        getAdminMsg(clazz).reduceMsg(num);
    }
    
    public static void reloadMsg(Class<? extends AdminMsg> clazz) {
        getAdminMsg(clazz).reloadMsg();
    }
}
